<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/10/9
 * Time: 15:48
 */

namespace app\circle\model;

use app\common\model\BaseModel;

class MeetingModel extends BaseModel
{
    protected $name = 'circle_meeting'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    //类型自动转换
    protected $type = [
        'content'=>'array',
        'start_time'=>'timestamp:Y-m-d H:i:s',
        'end_time'=>'timestamp:Y-m-d H:i:s',
        'sign_start_time'=>'timestamp:Y-m-d H:i:s',
        'sign_end_time'=>'timestamp:Y-m-d H:i:s'
    ];
    protected $append=['meeting_content'];

    /*读取器*/
    public function getStatusAttr($value, $data)
    {
        $status=['status'=>2,'text'=>'即将开始'];
        if ($data['start_time']<time()){
            $status=['status'=>0,'text'=>'正在进行'];
        }
        if ($data['end_time']<time()){
            $status=['status'=>1,'text'=>'已经结束'];
        }
        return $status;
    }
    public function getMeetingFocusTextAttr($value, $data)
    {
        if(!empty($data['meeting_focus'])){
            return explode('|',$data['meeting_focus']);
        }
        return [];
    }
    public function getMeetingStatusTextAttr($value,$data){
        $startTime = $data['start_time'];
        $endTime = $data['end_time'];
        $status = 0;
        if($endTime < time()){
            $status = 1;
        }
        if($startTime > time()){
            $status = 2;
        }
        return $status;
    }
    public function getCountAttr($value,$data){
        $meetingCompanyModel = new MeetingCompanyModel();
        $count = $meetingCompanyModel->where('meeting_id',$data['meeting_id'])->count();
        return $count;
    }
    /*
     * 参会企业
     */
    public function getCompanyListAttr($value,$data){
        $meetingId = $data['meeting_id'];
        $meetingCompanyModel = new MeetingCompanyModel();
        $companyList = $meetingCompanyModel->where('meeting_id',$meetingId)->select()->toArray();
        foreach ($companyList as $k=>$v){
            $companyList[$k]['company_intro']=get_field_value('circle_company', 'company_intro', $v['company_id']);
            $provId=get_field_value('circle_company', 'company_prov', $v['company_id']);
            $cityId=get_field_value('circle_company', 'company_city', $v['company_id']);
            $distId=get_field_value('circle_company', 'company_dist', $v['company_id']);
            $companyList[$k]['area_name']=getAddress($provId,$cityId,$distId);
        }
        //dump($companyList);
        return $companyList;
    }

    public function getMeetingContentAttr($value, $data)
    {
        $model=new MeetingContentModel();
        return $model->where('meeting_id',$data['meeting_id'])->limit(6)->select();
    }
    /*修改器*/
    
}